/*
 * @Author: xiawang1024
 * @Date: 2023-04-10 17:39:02
 * @LastEditTime: 2023-04-10 17:59:13
 * @LastEditors: xiawang1024
 * @Description:
 * @FilePath: /rollup-lib/config/builds.mjs
 * 工作，生活，健康
 */
import path from 'path'
import { readFile } from 'fs/promises'
import terser from '@rollup/plugin-terser'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

const pkg = JSON.parse(
  await readFile(new URL('../package.json', import.meta.url))
)

const banner = `/*!
 * WeChat.js v ${pkg.version}
 * (c) 2020-${new Date().getFullYear()} xiawang1024
 */`

const UMD_NAME = 'sdkName' //umd模式全局变量名
const LIB_NAME = 'hndt-demo' //cjs，esm 包名

const builds = [
  {
    banner,
    name: UMD_NAME,
    file: path.join(__dirname, '../dist', `${LIB_NAME}.${pkg.version}.js`),
    format: 'umd',
  },
  {
    banner,
    name: UMD_NAME,
    file: path.join(__dirname, '../dist', `${LIB_NAME}.${pkg.version}.min.js`),
    format: 'umd',
    plugins: [terser()],
  },
  {
    banner,
    file: path.join(__dirname, '../dist', `${LIB_NAME}.${pkg.version}.cjs.js`),
    format: 'cjs',
  },
  {
    banner,
    file: path.join(
      __dirname,
      '../dist',
      `${LIB_NAME}.${pkg.version}.cjs.min.js`
    ),
    format: 'cjs',
    plugins: [terser()],
  },
  {
    banner,
    file: path.join(__dirname, '../dist', `${LIB_NAME}.${pkg.version}.esm.js`),
    format: 'esm',
  },
  {
    banner,
    file: path.join(
      __dirname,
      '../dist',
      `${LIB_NAME}.${pkg.version}.esm.min.js`
    ),
    format: 'esm',
    plugins: [terser()],
  },
]

export default builds
